/**
 * @author haixuanwo
 * @email haixuanwo_clark@hotmail.com
 * @date 2021-03-25
 * @brief 系统类
 */

#include "system.h"
#include <iostream>
#include <unistd.h>

enum{
    LOGIN = 1,          // 登录
    REGISTER,           // 注册
    QUIT,               // 退出
};

enum{
    USER_MANAGE = 1,    // 用户管理
    STUDENT_MANAGE,     // 学生管理
    WORK_QUIT,          // 退出
};

enum{
    ADD = 1,            // 增
    DEL,                // 删
    SELECT,             // 查
    CHANGE,             // 改
    ACTION_QUIT         // 退出
};

System::System()
{
    userManager = new UserManager;
    studentManager  = new StudentManager;
}

void System::welcom_interface()
{
    std::cout<<"=============================================="<<std::endl;
    std::cout<<"=========== welcom to student system=========="<<std::endl;
    std::cout<<"=============================================="<<std::endl;
    std::cout<<"=============================================="<<std::endl;
}

void System::system_interface()
{
    std::cout<<"================================="<<std::endl;
    std::cout<<"=== please select ==="<<std::endl;
    std::cout<<"*** 1、login ***"<<std::endl;
    std::cout<<"*** 2、register ***"<<std::endl;
    std::cout<<"*** 3、quit ***"<<std::endl;
    std::cout<<"================================="<<std::endl;
}

void System::work_interface()
{
    std::cout<<"================================="<<std::endl;
    std::cout<<"=== please select ==="<<std::endl;
    std::cout<<"*** 1、user manage ***"<<std::endl;
    std::cout<<"*** 2、student manage ***"<<std::endl;
    std::cout<<"*** 3、quit ***"<<std::endl;
    std::cout<<"================================="<<std::endl;
}

void System::manage_interface()
{
    std::cout<<"================================="<<std::endl;
    std::cout<<"=== please select ==="<<std::endl;
    std::cout<<"*** 1、add ***"<<std::endl;
    std::cout<<"*** 2、delete ***"<<std::endl;
    std::cout<<"*** 3、select ***"<<std::endl;
    std::cout<<"*** 4、change ***"<<std::endl;
    std::cout<<"*** 5、quit ***"<<std::endl;
    std::cout<<"================================="<<std::endl;
}


bool System::login()
{
    char name[64] = {0};
    char passwd[64] = {0};

    std::cout<<"login name:"<<std::endl;
    scanf("%s", name);
    std::string str_name = name;

    std::cout<<"passwd:"<<std::endl;
    scanf("%s", passwd);
    std::string str_passwd = passwd;

    return userManager->match_user(str_name, &str_passwd);
}

bool System::register_account()
{
    char name[64] = {0};
    char passwd[64] = {0};
    int administrator = 0;

    std::cout<<"login name:"<<std::endl;
    scanf("%s", name);
    std::string str_name = name;

    std::cout<<"passwd:"<<std::endl;
    scanf("%s", passwd);
    std::string str_passwd = passwd;

    std::cout<<"administrator:"<<std::endl;
    scanf("%d", &administrator);

    return userManager->add_user(str_name, str_passwd, administrator);
}

/**
 * 用户管理
 * 增、删、查、改
 */
int System::user_manage()
{
    int action = USER_MANAGE;
    while(1)
    {
        manage_interface();
        scanf("%d", &action);
        switch(action)
        {
            case ADD:
            {
                register_account();
                break;
            }
            case DEL:
            {
                char name[64] = {0};
                std::cout<<"enter delete login name:"<<std::endl;
                scanf("%s", name);
                std::string str_name = name;
                userManager->delete_user(str_name);
                break;
            }
            case SELECT:
            {
                char name[64] = {0};
                std::cout<<"enter select login name:"<<std::endl;
                scanf("%s", name);
                std::string str_name = name;
                userManager->select_user(str_name);
                break;
            }
            case CHANGE:
            {
                char name[64] = {0};
                char passwd[64] = {0};
                int administrator = 0;

                std::cout<<"login name:"<<std::endl;
                scanf("%s", name);
                std::string str_name = name;

                std::cout<<"passwd:"<<std::endl;
                scanf("%s", passwd);
                std::string str_passwd = passwd;

                std::cout<<"administrator:"<<std::endl;
                scanf("%d", &administrator);
                userManager->change_user(str_name, str_passwd, administrator);
                break;
            }
            case ACTION_QUIT:
            {
                return 0;
            }
            default:
            {
                std::cout<<"incorrect input, please re-enter"<<std::endl;
                break;
            }
        }
    }
}


/**
 * 学生管理
 * 增、删、查、改
 */
int System::student_manage()
{
    int action = USER_MANAGE;
    while(1)
    {
        manage_interface();
        scanf("%d", &action);
        switch(action)
        {
            case ADD:
            {
                Student_t student;

                char name[64] = {0};
                std::cout<<"enter name:"<<std::endl;
                scanf("%s", name);
                student.name = name;

                int age = 0;
                std::cout<<"enter age:"<<std::endl;
                scanf("%d", &age);
                student.age = age;

                int studentID = 0;
                std::cout<<"enter studentID:"<<std::endl;
                scanf("%d", &studentID);
                student.studentID = studentID;

                int gender = 0;
                std::cout<<"enter gender:"<<std::endl;
                scanf("%d", &gender);
                student.gender = gender;

                int score = 0;
                std::cout<<"enter score:"<<std::endl;
                scanf("%d", &score);
                student.score = score;

                studentManager->add_student(student);
                break;
            }
            case DEL:
            {
                int studentID = 0;
                std::cout<<"enter studentID:"<<std::endl;
                scanf("%d", &studentID);
                studentManager->delete_student(studentID);
                break;
            }
            case SELECT:
            {
                int studentID = 0;
                std::cout<<"enter studentID:"<<std::endl;
                scanf("%d", &studentID);
                studentManager->select_student(studentID);
                break;
            }
            case CHANGE:
            {
                Student_t student;

                char name[64] = {0};
                std::cout<<"enter name:"<<std::endl;
                scanf("%s", name);
                student.name = name;

                int age = 0;
                std::cout<<"enter age:"<<std::endl;
                scanf("%d", &age);
                student.age = age;

                int studentID = 0;
                std::cout<<"enter studentID:"<<std::endl;
                scanf("%d", &studentID);
                student.studentID = studentID;

                int gender = 0;
                std::cout<<"enter gender:"<<std::endl;
                scanf("%d", &gender);
                student.gender = gender;

                int score = 0;
                std::cout<<"enter score:"<<std::endl;
                scanf("%d", &score);
                student.score = score;

                studentManager->change_student(student);
                break;
            }
            case ACTION_QUIT:
            {
                return 0;
            }
            default:
            {
                std::cout<<"incorrect input, please re-enter"<<std::endl;
                break;
            }
        }
    }
}



/**
 * 系统功能
 * 1、登录用户管理
 * 2、学生管理
 */
int System::work()
{
    int action = USER_MANAGE;
    while(1)
    {
        work_interface();
        scanf("%d", &action);
        switch(action)
        {
            case USER_MANAGE:
            {
                user_manage();
                break;
            }
            case STUDENT_MANAGE:
            {
                student_manage();
                break;
            }
            case WORK_QUIT:
            {
                return 0;
            }
            default:
            {
                std::cout<<"incorrect input, please re-enter"<<std::endl;
                break;
            }
        }
    }
}

int System::start()
{
    int action = LOGIN;

    welcom_interface();

    while(1)
    {
        system_interface();
        scanf("%d", &action);
        switch(action)
        {
            case LOGIN:
            {
                if (login())
                {
                    std::cout<<"login success"<<std::endl;
                    work();
                }
                else
                    std::cout<<"login fail, account or passwd incorrect"<<std::endl;

                break;
            }
            case REGISTER:
            {
                if (register_account())
                {
                    std::cout<<"register success"<<std::endl;
                    //work();
                }
                else
                    std::cout<<"register fail"<<std::endl;

                break;
            }
            case QUIT:
            {
                std::cout<<"system out, bye bye"<<std::endl;
                return 0;
            }
            default:
            {
                std::cout<<"incorrect input, please re-enter"<<std::endl;
                break;
            }
        }
    }

    return 0;
}

System::~System()
{
    delete userManager;
    userManager = nullptr;

    delete studentManager;
    studentManager = nullptr;
}

